from django.views import View
from django.http import JsonResponse
from app01.models import Avatars, Cover, UserInfo
from app01.models import avatar_delete, cover_delete
from django.db.models import Q
import base64
from api.utils.api_qiniu import upload_data


class AvatarView(View):
    def post(self, request):
        res = {
            'code': 414,
            'msg': '文件上传不合法',
        }
        if not request.user.is_superuser:
            res['msg'] = '用户验证失败'
            return JsonResponse(res)
        file = request.FILES.get('file')
        name = file.name
        file_type = ['jpg', 'jpeg', 'png']
        if name.split('.')[-1] not in file_type:
            return JsonResponse(res)

        kb = file.size / 1024 / 1024
        if kb > 2:
            res['msg'] = '文件大小超过2M'
            return JsonResponse(res)
        Avatars.objects.create(url=file)
        res['code'] = 0
        res['msg'] = 'success'
        return JsonResponse(res)

    def delete(self, request, nid):
        res = {
            'code': 322,
            'msg': '图片删除成功'
        }
        if not request.user.is_superuser:
            res['msg'] = '用户验证失败'
            return JsonResponse(res)
        avatar_query = Avatars.objects.filter(nid=nid)
        if not avatar_query:
            res['msg'] = '图片不存在'
            return JsonResponse(res)

        # 判断图片是否有人使用
        obj: Avatars = avatar_query.first()

        user_query = UserInfo.objects.filter(sign_status=1 | Q(sign_status=2))
        for user in user_query:
            if obj.url.url == user.avatar_url:
                res['msg'] = '该图片有人使用'
                return JsonResponse(res)
        avatar_delete(obj)
        avatar_query.delete()
        res['code'] = 0
        return JsonResponse(res)


class CoverView(View):
    def post(self, request):
        res = {
            'code': 414,
            'msg': '文件上传不合法',
        }
        if not request.user.is_superuser:
            res['msg'] = '用户验证失败'
            return JsonResponse(res)
        file = request.FILES.get('file')
        name = file.name
        file_type = ['jpg', 'jpeg', 'png']
        if name.split('.')[-1] not in file_type:
            return JsonResponse(res)

        kb = file.size / 1024 / 1024
        if kb > 2:
            res['msg'] = '文件大小超过2M'
            return JsonResponse(res)
        Cover.objects.create(url=file)
        res['code'] = 0
        res['msg'] = 'success'
        return JsonResponse(res)

    def delete(self, request, nid):
        res = {
            'code': 322,
            'msg': '图片删除成功'
        }
        if not request.user.is_superuser:
            res['msg'] = '用户验证失败'
            return JsonResponse(res)
        cover_query = Cover.objects.filter(nid=nid)
        if not cover_query:
            res['msg'] = '图片不存在'
            return JsonResponse(res)
        cover_delete(cover_query.first())
        cover_query.delete()
        res['code'] = 0
        return JsonResponse(res)


class PasteUploadView(View):
    def post(self, request):
        img = request.data.get('image')
        ines = img.split('base64,')
        # 转化图片数据
        imgData = base64.b64decode(ines[1])
        url = upload_data(imgData)
        return JsonResponse({'url':url})
